package small.common.designPattern.structure.proxy;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

/**
 * @Author zhoujin
 * @Date 2022/6/26 16:54
 */
public class ReflectiveMethodInvocation implements AopMethodInvocation {

    // 目标对象
    protected final Object target;
    // 目标类
    protected final Class<?> targetClass;
    // 方法
    protected final Method method;
    // 入参
    protected final Object[] arguments;

    public ReflectiveMethodInvocation(Object target,Class<?> targetClass, Method method, Object[] arguments) {
        this.target = target;
        this.targetClass = targetClass;
        this.method = method;
        this.arguments = arguments;
    }

    @Override
    public Method getMethod() {
        return method;
    }

    @Override
    public Object[] getArguments() {
        return arguments;
    }

    @Override
    public Object proceed() throws Throwable {
        if (target == null){
            return null;
        }
        return method.invoke(target, arguments);
    }

    @Override
    public Object getThis() {
        return target;
    }

    @Override
    public AccessibleObject getStaticPart() {
        return method;
    }

    @Override
    public Class<?> getTargetClass() {
        return targetClass;
    }
}
